﻿using System;
using System.Collections;
using System.Collections.Generic;
//using IBatisNet.DataAccess;
using Wsitech.Model;
using Wsitech.Utility;

namespace Wsitech.BLL
{
    /// <summary>
    /// 功能：     SiteMapInfo业务逻辑实现
    /// 作者：     LI
    /// 日期：     5/16/2012
    /// </summary>
    public class SiteMapInfoBLL
    {
        private Wsitech.Persistence.Interface.ISiteMapInfoDao dao = null;

        private SiteMapInfoBLL()
        {
            dao = Wsitech.Persistence.Dao.SiteMapInfoSqlMapDao.Instance;
        }

        #region （懒汉）单例模式
        private static SiteMapInfoBLL instance = null;
        private static object syncRoot = new object();
        public static SiteMapInfoBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new SiteMapInfoBLL();
                        }
                    }
                }
                return instance;
            }
        }
        #endregion


        #region	基本功能 不能修改


        /// <summary>
        /// 根据主键得到明细
        /// </summary>
        /// <returns></returns>
        public SiteMapInfo GetDetail(System.String sitemapid)
        {
            try
            {
                return dao.GetDetail(sitemapid);
            }
            catch
            {
                throw;
            }
        }


        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public IList<SiteMapInfo> GetList()
        {
            try
            {
                IList<SiteMapInfo> list = dao.GetList();
                return list;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 通过不同参数得到列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        private IList<SiteMapInfo> GetListByParams(object TopCounts, SiteMapInfo sitemapinfo)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                IList<SiteMapInfo> list = dao.GetListByParams(ht);
                return list;
            }
            catch
            {
                throw;
            }
        }



        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="sitemapinfo"></param>
        /// <returns></returns>
        public object Add(SiteMapInfo sitemapinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Add, GetDetail(sitemapinfo.SitemapId));
                return dao.Add(sitemapinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sitemapinfo"></param>
        /// <returns></returns>
        public int Update(SiteMapInfo sitemapinfo)
        {
            try
            {
                LogInfoBLL.Instance.AddLog(UtilityEnum.Operation.Update, GetDetail(sitemapinfo.SitemapId));
                return dao.Update(sitemapinfo);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="SitemapId"></param>
        /// <returns></returns>
        public int Delete(System.String sitemapid)
        {
            try
            {
                return dao.Delete(sitemapid);
            }
            catch
            {
                throw;
            }
        }

        #endregion


        #region 扩展功能

        /// <summary>
        /// 添加站点地图（前台菜单）
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="siteMapName"></param>
        /// <param name="frontUrl"></param>
        /// <returns>返回已经添加数据的编号</returns>
        public string AddSiteMap(string parentId, string siteMapName, string frontUrl, string backUrl)
        {
            SiteMapInfo map = new SiteMapInfo();

            map.SitemapId = Guid.NewGuid().ToString();
            map.SitemapName = siteMapName;
            map.ParentId = parentId;
            map.FrontUrl = frontUrl;
            map.BackUrl = backUrl;
            map.State = (int)UtilityEnum.State.Enable;
            map.IsDel = (int)UtilityEnum.Del.No;
            map.CreateTime = DateTime.Now;
            map.UpdateTime = DateTime.Now;
            map.SortIndex = dao.GetMaxIndexByParentId(parentId) + 1;




            Add(map);
            return map.SitemapId;
        }

        public IList<SiteMapInfo> GetSiteMapByParentId(string parentId)
        {
            Hashtable ht = new Hashtable();
            ht.Add("parentId", parentId);
            return dao.GetListByParams(ht);
        }
        #endregion

        #region 根据栏目编号获取栏目名称

        /// <summary>
        /// 获取栏目名称
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string GetSiteMapName(string sitemapid)
        {
            SiteMapInfo map = dao.GetDetail(sitemapid);
            if (map != null)
            {
                return map.SitemapName;
            }
            else
            {
                return "找不到该栏目";
            }
        }

        #endregion


        /// <summary>
        /// 显示到后台的父栏目
        /// 至少有一个子项目有后台路径
        ///父ID为""
        /// </summary>
        /// <returns></returns>
        public IList<SiteMapInfo> GetBackendParentList()
        {
            return dao.GetBackendParentList();
        }

        /// <summary>
        /// 根据父ID获取后台栏目
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public IList<SiteMapInfo> GetBackendListByParentId(string parentid)
        {
            Hashtable ht = new Hashtable();
            ht.Add("backend", true);
            ht.Add("state", (int)UtilityEnum.State.Enable);
            ht.Add("isDel", (int)UtilityEnum.Del.No);
            ht.Add("parentId", parentid);
            return dao.GetListByParams(ht);
        }
    }
}
